package com.gravata.enigma.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gravata.enigma.R;
import com.gravata.enigma.model.Service;

public class AlphabetListAdapter extends BaseAdapter {

	private Map<Integer, Boolean> selecionados=new HashMap<Integer, Boolean>();
	private int originalColor=-1;
    public static abstract class Row {}

    public static final class Section extends Row {
        public final String text;

        public Section(String text) {
            this.text = text;
        }
    }

    public static final class Item extends Row {
        public final Service servico;

        public Item(Service servico) {
            this.servico = servico;
        }
    }

    private List<Row> rows;

    public void setRows(List<Row> rows) {
        this.rows = rows;
    }

    public List<Row> getRows() {
		return rows;
	}

    @Override
    public int getCount() {
        return rows.size();
    }

    @Override
    public Row getItem(int position) {
        return rows.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position) instanceof Section) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        if (getItemViewType(position) == 0) { // Item
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.row_item_2, parent, false);
            }

            Item item = (Item) getItem(position);
            TextView textView = (TextView) view.findViewById(R.id.canal);
            textView.setText(item.servico.getName());
            view.setTag(item.servico);
            view.setBackgroundColor(getOriginalColor(view)); //default color
            if (selecionados.get(position) != null) {
            	view.setBackgroundColor(view.getResources().getColor(R.color.pressed_color));// this is a selected position so make it red
            }

        	TextView imageButton=(TextView) view.findViewById(R.id.hd_icon);

            if(item.servico.isHD()){
            	imageButton.setVisibility(View.VISIBLE);
            }else
            	imageButton.setVisibility(View.INVISIBLE);

            TextView satelite = (TextView) view.findViewById(R.id.satelite);
            TextView transponder = (TextView) view.findViewById(R.id.transponder);

            satelite.setText(item.servico.getTransponder().getSatelite().getName());
            transponder.setText(item.servico.getTransponder().toString());



        } else { // Section
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = (LinearLayout) inflater.inflate(R.layout.row_section, parent, false);
            }

            Section section = (Section) getItem(position);
            TextView textView = (TextView) view.findViewById(R.id.textView1);
            textView.setText(section.text);
        }

        return view;
    }

    public void setSelection(int position, boolean checked){
    	if(checked)
    		selecionados.put(position, checked);
    	else
        	selecionados.remove(position);

    	notifyDataSetChanged();
    }

    public void clearSelection(){
    	selecionados=new HashMap<Integer, Boolean>();
    	notifyDataSetChanged();
    }

    private int getOriginalColor(View view){
    	if(originalColor==-1){
	        originalColor = Color.TRANSPARENT;
	        Drawable background = view.getBackground();
	        if (background instanceof ColorDrawable)
	        	originalColor = ((ColorDrawable) background).getColor();
    	}
    	return originalColor;

    }

}
